import React from 'react';
import {StyleSheet, View, ScrollView} from 'react-native';
import Toast from 'react-native-easy-toast';

import api from '../../../common/api';
import auth from '../../../common/auth';

import Header from '../../../components/header';
import Content from './content';
import Rule from './rule';
import Bottom from './bottom';
import * as WeChat from "react-native-wechat";
import SharePlatform from "../../../share/SharePlatform";

export default class extends React.Component {
    state = {
        userInfo: {},
        currentIndex: 0,
        shareImgList: []
    };

    //获取数据
    getData() {
        api({url: 'api/member/getShareImg'})
            .then(data => {
                this.setState({
                    shareImgList: data || []
                });
            })
            .catch(e => {
                this.showToast(e.msg);
            });
    }

    //分享图片
    share = platform => {
        const {shareImgList, currentIndex} = this.state;
        if (!shareImgList.length) {
            this.showToast('请求中，请稍后');
            return;
        }
        let params = {
            type: 'imageUrl',
            title: '豆芽优选',
            description: '豆芽优选',
            mediaTagName: 'email signature',
            messageAction: undefined,
            messageExt: undefined,
            imageUrl: shareImgList[currentIndex]
        };
        WeChat.isWXAppInstalled()
            .then(isInstalled => {
                if (isInstalled) {
                    if (platform === SharePlatform.WECHATMOMENT) {
                        WeChat.shareToTimeline(params)
                            .then(() => {
                                alert('分享成功');
                            })
                            .catch(e => {
                                alert('分享失败');
                                console.log(e);
                            });
                    } else {
                        WeChat.shareToSession(params)
                            .then(() => {
                                alert('分享成功');
                            })
                            .catch(e => {
                                alert('分享失败');
                                console.log(e);
                            });
                    }
                } else {
                    this.showToast('没有安装微信软件，请您安装微信之后再试');
                }
            });
    };

    //设置当前图片
    setCurrentIndex = (idx) => {
        this.setState({
            currentIndex: idx
        })
    };

    //toast弹出
    showToast = (text, time = 1000) => {
        this.refs.toast.show(text, time);
    };

    componentDidMount() {
        auth.getUserInfo()
            .then(userInfo => {
                this.setState({
                    userInfo: userInfo
                }, () => {
                    this.getData();
                });
            })
            .catch(() => {
                auth.goBack();
            });
    }

    render() {
        const {userInfo, shareImgList} = this.state;
        return (
            <View style={styles.container}>
                <Header title="邀请好友"/>
                <ScrollView style={{flex: 1}}>
                    <Content shareImgList={shareImgList} setCurrentIndex={this.setCurrentIndex}/>
                    <Rule/>
                </ScrollView>
                <Bottom userInfo={userInfo} showToast={this.showToast} share={this.share}/>
                <Toast ref="toast" position="center"/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#fff',
        flex: 1
    }
});
